package mylibrary.io.jichu;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Class2 {

	public static void main(String[] args) throws Exception {
		
		Class2 class2 = new Class2();
		//class2.outputByPrintStream("d:\\test.txt");
		
		//class2.redirect();
		//class2.redirect1();
		
		//class2.printByBufferedReader();
		
		//class2.readByScanner();
		
		//class2.write("d:\\test.dat");
		
		//class2.zipFile("D:\\Movie\\IE6green.rar", "d:\\zip1.zip");
		
		class2.unzipFile("D:\\eclipse\\artifacts.zip", "d:\\zip1.xml");
	}
	
	
	/**
	 * 使用 PrintStream 进行输出
	 */
	public void outputByPrintStream(String outputPath) throws Exception {
		
		PrintStream print = new PrintStream(new FileOutputStream(new File(outputPath)));
		
		print.println(true);
		print.println("Rollen");
		
		print.close();
	}
	
	/**
	 * 输入输出重定向
	 */
	public void redirect() throws Exception {
		
		System.out.println("hello");
		
		File f = new File("d:\\test.txt");
		
		System.setOut(new PrintStream(new FileOutputStream(f)));
		
		System.out.println("This is the content in file");
	}
	
	public void redirect1() throws Exception {
		System.err.println("hello");
		File f = new File("d:\\test.txt");
		
		System.setErr(new PrintStream(new FileOutputStream(f)));
		System.err.println("This is the content in file");
		
		//System.setErr(null);
		System.err.println("hello");
	}
	
	/**
	 * 使用缓冲区从键盘上读入内容
	 * @throws Exception
	 */
	public void printByBufferedReader() throws Exception {
		
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Please input");
		
		String str = buf.readLine();
		System.out.println(str);
	}
	
	/**
	 * 使用Scanner从键盘读入内容
	 */
	public void readByScanner() {
		
		Scanner sca = new Scanner(System.in);
		//int temp = sca.nextInt();	
		//System.out.println(temp);
		
		System.out.println(sca.next());
	}
	
	/**
	 * DataOutputStream & DataInputStream
	 */
	public void write(String filePath) throws Exception {
		
		File file = new File(filePath);
		char[] ch = {'A', 'B', 'C'};
		
		DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
		
		for(char temp:ch) {
			out.write(temp);
		}
		
		out.close();
	}
	
	/**
	 * Zip out
	 */
	public void zipFile(String filePath, String destination) throws Exception {
		
		File file = new File(filePath);
		
		File zipFile = new File(destination);
		
		InputStream in = new FileInputStream(file);
		
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
		
		out.putNextEntry(new ZipEntry(file.getName()));
		
		out.setComment("Test");
		
		int temp = 0;
		
		while((temp=in.read()) != -1) {
			out.write(temp);
		}
		
		in.close();
		out.close();
	}
	
	/**
	 * unzip file
	 */
	public void unzipFile(String zipFilePath, String destination) throws Exception {
		
		File file = new File(zipFilePath);
		
		File outFile = new File(destination);
		
		ZipFile zipFile = new ZipFile(file);
		
		ZipEntry entry = zipFile.getEntry("artifacts.xml");
		
		InputStream input = zipFile.getInputStream(entry);
		
		OutputStream output = new FileOutputStream(outFile);
		
		int temp=0;
		
		while((temp=input.read()) != -1) {
			output.write(temp);
		}
		
		input.close();
		output.close();
		
		
	}
	
}




